﻿using Sustainalytics.LongRunningTaskManager.Interface;

namespace Sustainalytics.LongRunningTaskManager.Tests
{
    public abstract class MyTaskBase : ILongRunningTask
    {
        public abstract LongRunningTaskTypeEnum Type { get; }

        public virtual object Start(LongRunningTaskContext context)
        {
            MyTaskParam param = MyTaskParam.FromJson(context.ParamJson);

            if (param != null)
            {
                // set progress
                context.TaskStatus.Progress = param.Progress;

                // set errors
                if (param.Errors != null && param.Errors.Count > 0)
                {
                    foreach (var err in param.Errors)
                    {
                        context.TaskStatus.AddError(err);
                    }
                }

                // set delay
                if (param.DelayMs > 0)
                {
                    context.CancellationToken.WaitHandle.WaitOne(param.DelayMs);
                }
            }

            // notify cancellation
            context.CancellationToken.ThrowIfCancellationRequested();

            return null;
        }
    }
}
